package cn.edu.scau.cmi.tancuier.client;

import cn.edu.scau.cmi.tancuier.composite.consistent.ConsistentOrganization;
import cn.edu.scau.cmi.tancuier.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.tancuier.composite.consistent.ConsistentTeam;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ConsistentCompositeClient extends Application{

	public static void main(String[] args) {
		Application.launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("Consistent Composite");
		
		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10, 20, 10, 20));
		flowPane.setHgap(5);
		flowPane.setVgap(5);
		
		Label level = new Label("Higher Level:  ");
		flowPane.getChildren().add(level);
		TextField levelText = new TextField();
		levelText.setMinWidth(250);
		flowPane.getChildren().add(levelText);
		
		Label name = new Label("Name:               ");
		flowPane.getChildren().add(name);
		TextField nameText = new TextField();
		flowPane.getChildren().add(nameText);
		nameText.setMinWidth(250);
		
		Label select = new Label("Please choose the type you want to add: ");
		flowPane.getChildren().add(select);
		select.setMinWidth(300);
		
		ToggleGroup group = new ToggleGroup();
		RadioButton teamButton = new RadioButton("Team");
		teamButton.setToggleGroup(group);
		teamButton.setSelected(true);
		RadioButton personButton = new RadioButton("Person");
		personButton.setToggleGroup(group);
		flowPane.getChildren().add(teamButton);
		flowPane.getChildren().add(personButton);
		
		Button add = new Button("Add");
		flowPane.getChildren().add(add);
		
		Label space = new Label("");
		space.setPrefWidth(300);
		flowPane.getChildren().add(space);
		
		Label deleteName = new Label("The name to be delete: ");
		flowPane.getChildren().add(deleteName);
		
		TextField deleteText = new TextField();
		flowPane.getChildren().add(deleteText);
		
		Button delete = new Button("Delete");
		flowPane.getChildren().add(delete);
		
		ConsistentTeam root = new ConsistentTeam("Organization");
		root.setParent(null);
		add.setOnAction(e->{
			String childName, teamName;
			childName = nameText.getText();
			teamName = levelText.getText();
			if(group.getSelectedToggle() == personButton) {
				addChild(root, childName, teamName, false);
			}
			else if(group.getSelectedToggle() == teamButton) {
				addChild(root, childName, teamName, true);
			}
			
			System.out.println("Add " + childName + " to " + teamName);
			display(root, 0);
			System.out.println();
		});
		
		delete.setOnAction(e->{
			String childName = deleteText.getText();
			deleteChild(root, childName);
			
			System.out.println("Delete " + childName);
			display(root, 0);
			System.out.println();
		});
		
		Scene scene = new Scene(flowPane, 400, 400);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void addChild(ConsistentTeam node, String childName, String teamName, Boolean isTeam) {
		if(node.getName().equals(teamName)) {
			ConsistentOrganization newNode = isTeam ? new ConsistentTeam(childName) : new ConsistentPerson(childName);
			node.addChild(newNode);
			return;
		}
		for(ConsistentOrganization child : node.getChildren()) {
			if(child instanceof ConsistentTeam) {
				addChild((ConsistentTeam)child, childName, teamName, isTeam);
			}
		}
	}
	
	public static void deleteChild(ConsistentOrganization node, String name) {
		for(ConsistentOrganization child : ((ConsistentTeam)node).getChildren()) {
			if(child instanceof ConsistentPerson) {
				if(child.getName().equals(name)) {
					node.deleteChild(child);
					return;
				}
			}
			else if(child instanceof ConsistentTeam) {
				if(child.getName().equals(name)) {
					node.deleteChild(child);
					return;
				}
				deleteChild(child, name);
			}
		}
	}
	
	public static void display(ConsistentOrganization node, int depth) {
		depth++;
		if(node instanceof ConsistentPerson) {
			System.out.println(node.display(depth) + "(person)" + node.getName());
		}
		else if(node instanceof ConsistentTeam) {
			System.out.println(node.display(depth) + "(team)" + node.getName());
			for(ConsistentOrganization child : ((ConsistentTeam)node).getChildren()) {
				display(child, depth);
			}
		}
	}
	
}
